package cn.com.chengmandian.bike.rabbitmq;

import cn.com.chengmandian.bike.service.IEbPriceRuleService;
import cn.com.chengmandian.bike.service.IEbSettingAdService;
import cn.com.chengmandian.caffeine.constants.LocalCacheEnum;
import cn.com.chengmandian.core.redis.cache.AgentCacheService;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.rabbitmq.pojo.LocalCacheExpireNotify;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LocalCacheNotifyReceive {
    @Autowired
    private IEbSettingAdService settingAdService;

    @Autowired
    private IEbPriceRuleService priceRuleService;

    @Autowired
    private AgentCacheService agentCacheService;

    @RabbitListener(queues = "#{localCacheNotifyQueue.name}", ackMode = "AUTO")
    public void command(LocalCacheExpireNotify notify) {
        try {
            log.info("收到本地缓存更新通知:{}", JSONObject.toJSONString(notify));
            if (LocalCacheEnum.USER_AREA_AD_LIST.getCode().equals(notify.getCacheCode())) {
                LocalMapUtil.setAgentInfo(agentCacheService.getAgent(notify.getAgentId() + ""));
                settingAdService.loadCache(notify.getAgentId(), notify.getAreaId());
            } else if (LocalCacheEnum.USER_AREA_PRICE_RULE.getCode().equals(notify.getCacheCode())) {
                LocalMapUtil.setAgentInfo(agentCacheService.getAgent(notify.getAgentId() + ""));
                priceRuleService.loadCache(notify.getAgentId(), notify.getAreaId());
            } else {
                log.info("忽略本次通知");
            }
        } catch (Exception e) {
            log.error("LocalCacheNotifyReceive error!", e);
        }
    }
}
